package dk.isten.andro.main;

import java.util.ArrayList;

import util.Item;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import dk.isten.andro.R;


public class FrontActivity extends SuperActivity implements OnClickListener {
    /** Called when the activity is first created. */
	ImageView truppen, nyheder, stilling, ultras, kampprogram;
   ImageView roed, horn,gul, imageView1;
   MediaPlayer enLyd;
   Item item;
   static ArrayList<util.Item> news = new ArrayList<util.Item>();
   private String opdatering;
   String textnextgame;

   

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frontakt);
        
        TextView nextgame = (TextView) findViewById(R.id.nextgame);
		nextgame.setText("Næste hjemmekamp: \n" + Item.nextMatch );
		
        registerReceiver(NextGame, new IntentFilter(BaseApp.OPDATERINGSINTENT_NextGame));
        
    	//tjek om der er kommet en vigtig opdatering og fortæl brugeren hvis der er
        String testTekst = BaseApp.stamdata.json.optString("opdatering");
        Log.d("tjek for pdatering", testTekst);
       
        if(opdatering == null){
          		if(testTekst.equals("ja")){
        			Log.d("før toast", "tjek");
        			Toast.makeText(this, BaseApp.stamdata.json.optString("opdatering_text"), Toast.LENGTH_SHORT).show();
        			Log.d("efter toast", BaseApp.stamdata.json.optString("opdatering_text"));
        }
        }

		
        stilling = (ImageView) findViewById(R.id.stilling);
        truppen = (ImageView) findViewById(R.id.truppen);
        nyheder = (ImageView) findViewById(R.id.nyheder);
        ultras = (ImageView) findViewById(R.id.ultras);
        gul = (ImageView) findViewById(R.id.yellow);
        horn = (ImageView) findViewById(R.id.horn);
        roed = (ImageView) findViewById(R.id.red);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        kampprogram =(ImageView) findViewById(R.id.kampprogram);
		
		imageView1.setOnClickListener(this);
        stilling.setOnClickListener(this);
        truppen.setOnClickListener(this);
        nyheder.setOnClickListener(this);
        ultras.setOnClickListener(this);
        gul.setOnClickListener(this);
        horn.setOnClickListener(this);
        roed.setOnClickListener(this);
        kampprogram.setOnClickListener(this);

        
	}
	@Override
	public void onClick(View v) {

			if(v==imageView1){
            
        	Intent i = new Intent(this, FrontActivity.class);
                             startActivity(i);
                    
        }else if(v==stilling){

                	Intent i = new Intent(this, Stilling_List_Akt.class);
                   Log.d("Intent", "Starter Stilling_ListAkt");
                            startActivity(i);
                            
                }else if(v==truppen){
                	Intent i = new Intent(this, Truppen_Akt.class);
                    
                    startActivity(i);
                }else if(v==nyheder){
                	Intent i = new Intent(this, Nyheder_Akt.class);
                    
                    startActivity(i);
                }else if(v==ultras){
                	Intent i = new Intent(this, Ultras_Akt.class);
                    
                    startActivity(i);
                }else 
			if(v==gul){
                
                	Intent i = new Intent(this, yellow.class);
                   
                            startActivity(i);
                            
                }else if(v==roed){
                	Intent i = new Intent(this, red.class);
                    
                    		startActivity(i);
                }else if(v==horn){
                	
                    setVolumeControlStream(AudioManager.STREAM_MUSIC);
                    AudioManager audiomanager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
                    //skru op for lyden
                    int  maxLydStyrke = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

                    audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, maxLydStyrke, AudioManager.FLAG_SHOW_UI);
                    
                    
                	MediaPlayer enLyd = MediaPlayer.create(this, R.raw.vuvuzela);
                    enLyd.setVolume(9, 9);
                    enLyd.start();
                } else if (v==kampprogram){
                	Intent i = new Intent(this, Kampprogram_List_Akt.class);
//                	System.out.println("så er Kampprogram_List_Akt kaldt");
          		startActivity(i);
              }
                }
	
	 //Nedenstående er broadcast receiver på asynctask
    private BroadcastReceiver NextGame = new BroadcastReceiver() {

		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Item item = new Item();
			textnextgame = Item.nextMatch;
			TextView nextgame = (TextView) findViewById(R.id.nextgame);
			nextgame.setText("Næste hjemmekamp: " + textnextgame );
			
		}
		
	};
	
}